<?php

namespace App\Http\Controllers;

use App\Models\PermissionCategory;
use Illuminate\Http\Request;

class PermissionCategoryController extends Controller
{
    public function index()
    {
        $q = PermissionCategory::query();
        if (\request()->get('start')) {
            $q->where('created_at', '>=', \request()->get('start'));
        }
        if (\request()->get('end')) {
            $q->where('created_at', '<=', \request()->get('end'));
        }
        if (\request()->get('category-name')) {
            $q->where('name', 'like', "%".\request()->get('category-name')."%");
        }
        $permissionCategories = $q->withCount('permissions')->get();
        \request()->flash();
        if (\request()->wantsJson()) {
            return response()->json(['code' => 0, 'msg' => '', 'count' => count($permissionCategories), 'data' => $permissionCategories]);
        }
        return view('rbac.permissionCategories.index', compact('permissionCategories'));
    }

    public function create()
    {
        return view('rbac.permissionCategories.create');
    }

    public function store(Request $request)
    {
        try {
            $permissionCategory = PermissionCategory::create($request->all());
            $response = [
                'message' => 'PermissionCategory created.',
                'data' => $permissionCategory->toArray(),
            ];

            if ($request->wantsJson()) {
                return response()->json($response);
            }

            return redirect()->back()->with('message', $response['message']);
        } catch (\Exception $e) {
            if ($request->wantsJson()) {
                return response()->json([
                    'error' => true,
                    'message' => $e->getMessage()
                ]);
            }
            return redirect()->back()->withErrors($e->getMessage())->withInput();
        }
    }

    public function edit($id){
        $permissionCategory = PermissionCategory::find($id);
        return view('rbac.permissionCategories.edit',compact('permissionCategory'));
    }

    public function update(Request $request,$id){
        try {
            $permissionCategory = PermissionCategory::find($id);
            $permissionCategory->update($request->all());
            $response = [
                'message' => 'PermissionCategory updated.',
                'data'    => $permissionCategory->toArray(),
            ];

            if ($request->wantsJson()) {
                return response()->json($response);
            }

            return redirect()->back()->with('message', $response['message']);
        } catch (\Exception $e) {

            if ($request->wantsJson()) {

                return response()->json([
                    'error'   => true,
                    'message' => $e->getMessage()
                ]);
            }
            return redirect()->back()->withErrors($e->getMessage())->withInput();
        }
    }

    public function destroy($id){
        $permissionCategory = PermissionCategory::find($id);
        $deleted = $permissionCategory->delete();
        $permissionCategory->permissions()->detach();
        if (request()->wantsJson()) {
            return response()->json([
                'message' => 'PermissionCategory deleted.',
                'deleted' => $deleted,
            ]);
        }

        return redirect()->back()->with('message', 'PermissionCategory deleted.');
    }

}
